package com.run_java_test.test4;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/controller")
public class ControllerManager {

    private final ExternalControllerLoader externalControllerLoader;

    public ControllerManager(ExternalControllerLoader externalControllerLoader) {
        this.externalControllerLoader = externalControllerLoader;
    }

    /**
     * 动态注册 Controller
     *
     * @param beanName     Bean 的名称
     * @param javaFilePath Java 文件的路径
     * @return 操作结果
     */
    @GetMapping("/register")
    public String registerController(@RequestParam String beanName, @RequestParam String javaFilePath) {
        try {
            externalControllerLoader.registerController(beanName, javaFilePath);
            return "Controller 注册成功。";
        } catch (Exception e) {
            return "Controller 注册失败: " + e.getMessage();
        }
    }

    /**
     * 动态卸载 Controller
     *
     * @param beanName Bean 的名称
     * @return 操作结果
     */
    @GetMapping("/unregister1")
    public String unregisterController1(@RequestParam String beanName) {
        try {
            externalControllerLoader.unregisterController(beanName);
            return "Controller 卸载成功。";
        } catch (Exception e) {
            return "Controller 卸载失败: " + e.getMessage();
        }
    }
}
